#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('replace-input');

my $n_tests = 8;

# Use Unicode file names to test replace input so we can be sure it
# works for that case.
$td->runtest("create unicode filenames",
             {$td->COMMAND => "test_unicode_filenames"},
             {$td->STRING => "created Unicode filenames\n",
                  $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);

foreach my $d (['auto-ü', 1], ['auto-öπ', 2])
{
    my ($u, $n) = @$d;
    $td->runtest("replace input $u",
                 {$td->COMMAND => "qpdf --deterministic-id" .
                      " --object-streams=generate --replace-input ./$u.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    # qpdf handles Unicode filenames on Windows, but qpdf-test-compare
    # doesn't.
    rename("$u.pdf", "u.pdf") or die;
    $td->runtest("check output ($u)",
                 {$td->COMMAND => "qpdf-test-compare u.pdf replace-input.pdf"},
                 {$td->FILE => "replace-input.pdf", $td->EXIT_STATUS => 0});
}

system("cp xref-with-short-size.pdf auto-warn.pdf") == 0 or die;
$td->runtest("replace input with warnings",
             {$td->COMMAND =>
                  "qpdf --deterministic-id --replace-input ./auto-warn.pdf"},
             {$td->FILE => "replace-warn.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

$td->runtest("check output",
             {$td->COMMAND => "qpdf-test-compare auto-warn.pdf warn-replace.pdf"},
             {$td->FILE => "warn-replace.pdf", $td->EXIT_STATUS => 0});
$td->runtest("check orig output",
             {$td->FILE => "auto-warn.pdf.~qpdf-orig"},
             {$td->FILE => "xref-with-short-size.pdf"});

cleanup();
$td->report($n_tests);
